package perf.cli;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

import perf.data.Run;
import perf.data.Setup;
import perf.parse.valgrind.Parser;
import perf.store.ObjectStore;

public class Executor {

	private ObjectStore store = null;
	
	public Executor( ) {
	}
	
	public void startDb( File f ) {
		store = new ObjectStore( f );
	}

	public void listSetup( ) {
		List<Setup> setups = store.list().setups();
		
		for ( int i = 0; i < setups.size(); i++ ) {
			System.out.println( setups.get( i ).toString() );
		}
	}
	
	public void addSetup( String name ) {
		Setup setup = new Setup( name );
		store.modify().addSetup( setup );
	}
	
	public void removeSetup( int idx ) {
		store.modify().deleteSetup( idx );
	}
	
	public void listRun( int setupIdx ) {
		List<Run> results = store.list().runs( setupIdx ); 
			
		for ( int i = 0; i < results.size(); i++) {
			System.out.println( i + " " + results.get(i).getRunDate() );
		}
	}

	public void addRun( File valgrind, int setupIdx ) throws FileNotFoundException {
		Parser parser = new Parser( );
		
		Run run = parser.parse( valgrind );
		store.modify().addRun( setupIdx, run );
	}

	public void removeRun( int runIdx, int setupIdx ) {
		store.modify().deleteRun( runIdx, setupIdx );
	}
	
	public void dump( ) {
		List<Setup> setups = store.list().setups();
		System.out.println( "Sessions: " + setups.size() );
		for ( Setup s : setups ) {
			System.out.println( s.toString() );
		}
	}

	public void close( ) {
		if ( store != null ) {
			store.close();			
		}
	}
}
